package org.jfk.core.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.ehcache.CacheManager;

import com.jfinal.core.Controller;
import com.jfinal.plugin.ehcache.CacheKit;

public class EhCacheController extends BaseController{
	public void index(){
	}
	
	public void clearAllCache(){
		CacheManager cm = CacheKit.getCacheManager();
		String[] cacheNames = cm.getCacheNames();
		List<Map> list = new ArrayList();
		for (int i = 0 ; cacheNames != null && i < cacheNames.length; i++){
			String cacheName = cacheNames[i];
			CacheKit.removeAll(cacheName);
		}
		this.returnSuc("ok");
	}
	
	public void contentInfo4ztree(){
		CacheManager cm = CacheKit.getCacheManager();
		String[] cacheNames = cm.getCacheNames();
		List<Map> list = new ArrayList();
		for (int i = 0 ; cacheNames != null && i < cacheNames.length; i++){
			String cacheName = cacheNames[i];
			Map cache = new HashMap();
			cache.put("id", cacheName);
			cache.put("pId", 0);
			cache.put("name", cacheName);
			cache.put("open", "true");
			list.add(cache);
			List keys = CacheKit.getKeys(cacheName);
			
			for (int j = 0 ; keys != null && j < keys.size() ; j++){
				Object key = keys.get(j);
				Map data = new HashMap();
				data.put("id", i+"-"+j);
				data.put("pId", cacheName);
				data.put("name", key.toString() + " - " + key.getClass().getName()+" ==> " + CacheKit.get(cacheName, key));
				data.put("open", "true");
				list.add(data);
			}
		}
		this.renderJson(list);
	}	
}
